﻿using System;
using System.Collections.Generic;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using MimeKit.Text;


namespace EmailManager
{
    public class EmailSender
    {
        private static void SendEmail(IEnumerable<string> to, string subject, string body, string user, string from, string authentication, TextFormat type = TextFormat.Plain)
        {
            var msg = new MimeMessage()
            {
                Sender = new MailboxAddress(user, from),
                Subject = subject
            };
            foreach (var s in to)
            {
                msg.To.Add(new MailboxAddress(s));
            }

            msg.From.Add(new MailboxAddress(user, from));
            msg.Body = new TextPart(type)
            {
                Text = body
            };

            using var smtp = new SmtpClient
            {
                ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true
            };
            smtp.Connect("smtp.qq.com", 587, SecureSocketOptions.StartTls);
            smtp.Authenticate(@from, authentication);
            smtp.Send(msg);
        }

        public static void SendEmail(IEnumerable<string> to, string subject, string body)
        {
            SendEmail(to, subject, body, Config.SenderName, Config.Email, Config.AuthCode);
        }
    }
}